package com.yanqu.road.dao.impl.activity.newyearbag;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.newyearbag.NewYearBagLimitData;
import com.yanqu.road.entity.activity.newyearbag.NewYearBagPool;
import com.yanqu.road.entity.activity.newyearbag.NewYearBagShiWuLimitData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class NewYearBagPoolDaoImpl extends TempDataObjectDao<NewYearBagPool> {

    public static String getNewYearBagPoolSql = "select * from `t_c_new_year_bag_pool` where `activityId`=?";

    public NewYearBagPool getNewYearBagPool(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getNewYearBagPoolSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            if (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String updateNewYearBagPoolSql = "update `t_c_new_year_bag_pool` set limitDataMap=?,shiWuLimitDataMap=? where activityId=?";

    @Override
    protected boolean updateData(NewYearBagPool newYearBagPool) {
        newYearBagPool.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(newYearBagPool.getLimitDataMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(newYearBagPool.getShiWuLimitDataMap())));
        para.put(i++, new DbParameter(Types.INTEGER, newYearBagPool.getActivityId()));
        if (getDb().executeNoneQuery(updateNewYearBagPoolSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertNewYearBagPoolSql = "insert into `t_c_new_year_bag_pool`(activityId,limitDataMap,shiWuLimitDataMap) values (?,?,?)";

    @Override
    protected boolean addData(NewYearBagPool newYearBagPool) {
        newYearBagPool.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, newYearBagPool.getActivityId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(newYearBagPool.getLimitDataMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(newYearBagPool.getShiWuLimitDataMap())));
        if (getDb().executeNoneQuery(insertNewYearBagPoolSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected NewYearBagPool parseData(ResultSet rs) throws SQLException {
        NewYearBagPool newYearBagPool = new NewYearBagPool();
        newYearBagPool.setActivityId(rs.getInt("activityId"));
        newYearBagPool.setLimitDataMap(JSON.parseObject(rs.getString("limitDataMap"), new TypeReference<ConcurrentHashMap<Integer, NewYearBagLimitData>>() {
        }));
        newYearBagPool.setShiWuLimitDataMap(JSON.parseObject(rs.getString("shiWuLimitDataMap"), new TypeReference<ConcurrentHashMap<Integer, NewYearBagShiWuLimitData>>() {
        }));
        return newYearBagPool;
    }

}
